//
//  UIButton+Addition.m
//  RenRenLeXiang
//
//  Created by MyMac on 2017/4/5.
//  Copyright © 2017年 MyMac. All rights reserved.
//

#import "UIButton+Addition.h"
#import <objc/runtime.h>

@interface UIButton ()
@property(nonatomic,strong)NSMutableDictionary * backgroundColorsForStates;
@end

@implementation UIButton (Addition)




- (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state{
    if (!color) {return;}
    
    CGFloat imageWH = 28;
    //开启图形上下文
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageWH, imageWH), NO, [UIScreen mainScreen].scale);
    //绘制当前页图片
    CGContextRef context = UIGraphicsGetCurrentContext();
    //设置填充色
    CGContextSetFillColorWithColor(context, color.CGColor);
    //添加圆
//    CGContextAddArc(context, imageWH/2, imageWH/2, imageWH/2, 0, M_PI*2, 0);
    CGContextAddRect(context, CGRectMake(0, 0, imageWH, imageWH));
    //绘制
    CGContextDrawPath(context, kCGPathFill);
    //获取图片
    UIImage* currentImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    
    [self setBackgroundImage:[currentImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 14, 0, 14) resizingMode:UIImageResizingModeStretch] forState:state];
}



@end
